import Vue from 'vue'
import Vuex from 'vuex'
import itemData from '@/assets/upload/cart.json'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list: itemData.data,
    payPrice: 0
  },
  mutations: {
    del (state, Id) {
      try {
        const index = state.list.findIndex(item => item.productId === Id)
        state.list.splice(index, 1)
      } catch (err) {
        console.log(err)
      }
    },
    allChecked (state) {
      state.list.forEach(item => { item.checked = true })
    },
    cancel (state) {
      state.list.forEach(item => { item.checked = false })
    }
  },
  getters: {
    totalPrice (state) {
      const checkedItems = state.list.filter(item => item.checked === true)
      let total = 0
      checkedItems.forEach(item => { total += item.productPrice * item.productNum })
      return total
    },
    allCheckedState (state) {
      const result = state.list.find(item => item.checked === false)
      if (!result) return true
      return false
    }
  }
})
